package com.cc.dlnalib;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import org.cybergarage.upnp.Device;

import java.util.List;

import kotlin.Unit;
import kotlin.jvm.functions.Function1;

public class DLNAUtil {
    private static final String MEDIARENDER = "urn:schemas-upnp-org:device:MediaRenderer:1";

    /**
     * Check if the device is a media render device
     *
     * @param device 设备
     * @return 是否可用
     */
    public static boolean isMediaRenderDevice(Device device) {
        return device != null
                && MEDIARENDER.equalsIgnoreCase(device.getDeviceType());
    }

    public static void showDLNADialog(Activity activity, Function1<Device, Void> function) {
        ListView listView = new ListView(activity);
        AlertDialog alertDialog = new AlertDialog.Builder(activity).setView(listView).create();
        alertDialog.setTitle("请选择投屏设备");
        Function1<List<Device>, Unit> function1 = devices -> {
            listView.setAdapter(new BaseAdapter() {
                @Override
                public int getCount() {
                    return devices.size();
                }

                @Override
                public Device getItem(int position) {
                    return devices.get(position);
                }

                @Override
                public long getItemId(int position) {
                    return 0;
                }

                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                    TextView textView =
                            (TextView) LayoutInflater.from(activity).inflate(android.R.layout.simple_list_item_1, parent, false);
                    textView.setText(getItem(position).getFriendlyName());
                    return textView;
                }
            });
            listView.setOnItemClickListener((parent, view, position, id) -> {
                function.invoke(devices.get(position));
                alertDialog.dismiss();
            });
            return null;
        };
        alertDialog.setOnDismissListener(dialog -> DLNAController.INSTANCE.removeCallBack(function1));
        alertDialog.show();
        DLNAController.INSTANCE.addCallBack(function1);
        DLNAController.INSTANCE.startSearch();
    }

    /**
     * 将ip的整数形式转换成ip形式
     *
     * @param ipInt
     * @return
     */
    private static String int2ip(int ipInt) {
        return (ipInt & 0xFF) + "." +
                ((ipInt >> 8) & 0xFF) + "." +
                ((ipInt >> 16) & 0xFF) + "." +
                ((ipInt >> 24) & 0xFF);
    }

    /**
     * 获取当前ip地址
     *
     * @param context
     * @return
     */
    public static String getLocalIpAddress(Context context) {
        try {
            WifiManager wifiManager = (WifiManager) context.getApplicationContext()
                    .getSystemService(Context.WIFI_SERVICE);
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            int i = wifiInfo.getIpAddress();
            return int2ip(i);
        } catch (Exception ex) {
            return "";
        }
    }
}
